﻿using RAP.Framework.Libary.DomTemplateBuilder.Element.Conditional;
using System.Collections;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.Conditional
{
	/// <summary>
	/// 检测参数对象是否只包含指定值的Handler
	/// </summary>
	public class IsOnlyContainValuesHandler : IsContainValuesHandler
    {
        public new IsOnlyContainValues Element
        {
            get
            {
				return base.Element as IsOnlyContainValues;
            }
        }

		protected override bool DoCompareValue ( object propertyValue, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			var value = propertyValue;
			ICollection values = null;

			if ( value == null )
			{
				return false;
			}
			ICollection compareValues = null;
			var compareValue = this.GetCompareValue ( rootParamObject, parentParamObject, context );
			if ( compareValue is string )
			{
				compareValues = compareValue.ToString ( ).Split ( ',' );
			}
			else if ( compareValue is ICollection )
			{
				compareValues = compareValue as ICollection;
			}
			else if ( compareValue is IDictionary )
			{
				compareValues = ( compareValue as IDictionary ).Values;
			}

			if ( value is string )
			{
				values = value.ToString ( ).Split ( ',' );
			}
			else if ( value is ICollection )
			{
				values = value as ICollection;
			}
			else if ( value is IDictionary )
			{
				values = ( value as IDictionary ).Values;
			}

			foreach ( var value1 in values )
			{
				var findResult = false;
				foreach ( var value2 in compareValues )
				{
					if ( value1.Compare ( value2 ) == 0 )
					{
						findResult = true;
						break;
					}
				}
				if ( !findResult )
				{
					return false;
				}
			}
			return true;
		}
    }
}
